package com.longkey.workflow.client.view
{
	import com.longkey.workflow.client.control.Context;
	import com.longkey.workflow.client.control.Model;
	import com.longkey.workflow.client.event.TemplateEvent;
	import com.longkey.workflow.client.model.TemplateModel;
	
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.controls.Tree;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.controls.treeClasses.TreeItemRenderer;
	import mx.events.ListEvent;
	
	import org.longkey.portal.PHPSerializer;
	import org.longkey.portal.Portal;
	
	[Event(name="updateTemplate", type="com.longkey.workflow.client.event.TemplateEvent")]
	public class WFNavigator extends Tree
	{

		private var currentNode:Object;
		
		[Embed(source="/img/navigator/folder.gif")]
		private static var folderIcon:Class;
		
		[Embed(source="/img/navigator/file.gif")]
		private static var fileIcon :Class;
		
		public function WFNavigator()
		{
			super();
			Context.navigator = this;
			this.init();
		}
		
		private function init():void{
			this.percentHeight = 100;
			this.percentWidth = 100;
			this.doubleClickEnabled = true;
			this.editable = true;
			this.editorDataField = "text";
			this.labelField = "name";
			this.iconFunction = iconFun;
//			this.itemRenderer = new NavigatorItemRenderFactory();
			this.initEventListener();
			this.initContextMenu();
			
//			Portal.execjava(setDataProvider, "WFTemplateBean.getRoot");
			
			var demoresult:String = 'm:7:{s:2:"id";s:1:"0";s:8:"parentId";N;s:4:"name";s:3:"工作流";s:5:"order";i:0;s:5:"index";i:0;s:8:"nodeType";i:0;s:8:"children";a:1:{i:0;m:16:{s:2:"id";s:2:"23";s:8:"parentId";s:1:"0";s:4:"name";s:4:"dddd";s:5:"order";i:1;s:5:"index";i:0;s:8:"nodeType";i:0;s:10:"templateId";d:0;s:12:"createUserId";d:0;s:10:"createTime";d:1385568000000;s:12:"updateUserId";d:0;s:10:"updateTime";d:1385568000000;s:12:"deployUserId";N;s:10:"deployTime";N;s:14:"createUserName";N;s:14:"updateUserName";N;s:8:"children";a:3:{i:0;m:16:{s:2:"id";s:2:"24";s:8:"parentId";s:2:"23";s:4:"name";s:7:"测试-请假流程";s:5:"order";i:1;s:5:"index";i:0;s:8:"nodeType";i:1;s:10:"templateId";d:0;s:12:"createUserId";d:0;s:10:"createTime";d:1385568000000;s:12:"updateUserId";d:0;s:10:"updateTime";d:1385654400000;s:12:"deployUserId";N;s:10:"deployTime";N;s:14:"createUserName";N;s:14:"updateUserName";N;s:8:"children";a:0:{}}i:1;m:16:{s:2:"id";s:2:"25";s:8:"parentId";s:2:"23";s:4:"name";s:13:"请假流程addsfadsf";s:5:"order";i:2;s:5:"index";i:1;s:8:"nodeType";i:1;s:10:"templateId";d:0;s:12:"createUserId";d:0;s:10:"createTime";d:1385654400000;s:12:"updateUserId";d:0;s:10:"updateTime";d:1385654400000;s:12:"deployUserId";N;s:10:"deployTime";N;s:14:"createUserName";N;s:14:"updateUserName";N;s:8:"children";a:0:{}}i:2;m:16:{s:2:"id";s:2:"26";s:8:"parentId";s:2:"23";s:4:"name";s:4:"发文流程";s:5:"order";i:3;s:5:"index";i:2;s:8:"nodeType";i:1;s:10:"templateId";d:0;s:12:"createUserId";d:0;s:10:"createTime";d:1386691200000;s:12:"updateUserId";d:0;s:10:"updateTime";d:1386691200000;s:12:"deployUserId";N;s:10:"deployTime";N;s:14:"createUserName";N;s:14:"updateUserName";N;s:8:"children";a:0:{}}}}}}';
			var demoData:Object = PHPSerializer.unserialize(demoresult);
			setDataProvider(demoData);
		}
		
		private function initEventListener():void{
//			this.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
			this.addEventListener(ListEvent.ITEM_EDIT_BEGINNING, itemEditBeginningHandler);
			this.addEventListener(ListEvent.ITEM_EDIT_END, itemEditEndHandler);
			this.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, doubleClickItemHandler);
			this.addEventListener("updateTemplate", updateTemplate);
			
		}
		
		private function initContextMenu():void{
			
			var ctx:ContextMenu = new ContextMenu();
			ctx.hideBuiltInItems();
			var menu:ContextMenuItem = new ContextMenuItem("新增目录");
			menu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, createFolder);
			ctx.customItems.push(menu);
			
			
			
			menu = new ContextMenuItem("新建流程");
			menu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, createTemplate);
			ctx.customItems.push(menu);
			
			menu = new ContextMenuItem("重命名");
			menu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, editNode);
			ctx.customItems.push(menu);
			
			menu = new ContextMenuItem("删除");
			menu.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, deleteNode);
			ctx.customItems.push(menu);
			
			ctx.addEventListener(ContextMenuEvent.MENU_SELECT, updateContextMenu);
			
//			this.listContent.contextMenu = ctx;
			this.contextMenu = ctx;
		}
		
		private function setDataProvider(data:Object):void{
			if(data == null || data is Boolean){
				data = {};
				data["name"] = "网络出错";
				data["nodeType"] = 0;
				data["indext"] = 0;
				data["id"] = 0;
			}
			this.dataProvider = data;
			this.validateNow();
			this.expandItem(this.dataProvider[0], true, false);
		}
		
		private function updateContextMenu(event:ContextMenuEvent):void{
			var item:TreeItemRenderer = event.mouseTarget as TreeItemRenderer;
			var ary:Array;
			if(item.data["nodeType"] == 0){//branch
				ary = [true, true, true, true];
			}
			else{
				ary = [true, true, false, false];
			}
			for each(var menu:ContextMenuItem in this.contextMenu.customItems){
				menu.visible = ary.pop() as Boolean;
			}
		}
		
		private function createFolder(event:ContextMenuEvent):void{
			var item:TreeItemRenderer = event.mouseTarget as TreeItemRenderer;
			this.currentNode = item.data;
			var params:Object = {};
			params["parentId"] = item.data["id"];
			params["name"] = "新增目录";
			Portal.execjava(addNode, "WFTemplateBean.createDir",params);
		}
		
		private function createTemplate(event:ContextMenuEvent):void{
			var item:TreeItemRenderer = event.mouseTarget as TreeItemRenderer;
			this.currentNode = item.data;
			var params:Object = {};
			params["parentId"] = item.data["id"];
			params["name"] = "新建流程";
			Portal.execjava(addNode, "WFTemplateBean.createTemplate",params);
		}
		
		private function addNode(node:Object):void{
			if(this.currentNode != null && node !=null){
				this.dataDescriptor.addChildAt(this.currentNode, node, node["index"]);
			}
		}
		
		private function editNode(event:ContextMenuEvent):void{
			var item:TreeItemRenderer = event.mouseTarget as TreeItemRenderer;
			this.currentNode = item.data;
			var pos:Point = itemRendererToIndices(item);
			var listEvent:ListEvent = new ListEvent(ListEvent.ITEM_EDIT_BEGINNING, false, true);
			
			listEvent.rowIndex = pos.y;
			listEvent.columnIndex = 0;
			listEvent.itemRenderer = item;
			listEvent.reason = "rename";
			dispatchEvent(listEvent);
		}
		
		private function deleteNode(event:ContextMenuEvent):void{
			var item:TreeItemRenderer = event.mouseTarget as TreeItemRenderer;
			this.currentNode = item.data;
			var params:Object = {};
			params["id"] = item.data["id"];
			params["delTag"] = "Y";
			Portal.execjava(removeNode, "WFTemplateBean.updateNode",params);
		}
		
		private function removeNode(node:Object):void{
			if(node == null)
				return;
			var parent:Object = this.getParentItem(this.currentNode);
			if(parent == null)
				return;
			this.dataDescriptor.removeChildAt(parent, this.currentNode, this.currentNode["index"]);
			this.currentNode = null;
		}
		
		private function iconFun(item:Object):Class{
			if(item["nodeType"] == 0){
				return folderIcon;
			}
			else{
				return fileIcon;
			}
		}
		
		private function updateTemplate(event:TemplateEvent):void{
			
		}
		
		protected function itemEditBeginningHandler(event:ListEvent):void
		{
			if(event.rowIndex == 0 || event.reason != "rename"){
				event.preventDefault();
			}
		}
		
		protected function itemEditEndHandler(event:ListEvent):void
		{
			var item:IListItemRenderer = event.itemRenderer;
			if(this.itemEditorInstance["text"] != item.data["name"]){
				var params:Object = {};
				params["id"] = item.data["id"];
				params["name"] = this.itemEditorInstance["text"];
				var templateModel:TemplateModel = Model.getInstance().getTemplate(params["id"]);
				if(templateModel != null){
					templateModel.templateName = params["name"];
				}
				Portal.execjava(null, "WFTemplateBean.updateNode", params);
				if(item.data["nodeType"] == 1){
					var e:TemplateEvent = new TemplateEvent("renameTemplate");
					e.templateId = params["id"];
					e.templateName = params["name"];
					Context.container.dispatchEvent(e);
				}
			}
		}
		
		protected function doubleClickItemHandler(event:ListEvent):void{
			var item:IListItemRenderer = event.itemRenderer;
			if(item.data.nodeType == 0){
				var ary:Array = this.openItems as Array;
				var open:Boolean = true;
				for each(var one:Object in ary){
					if(one["id"] == item.data["id"]){
						open = false;
						break;
					}
				}
				this.expandItem(item.data, open);
			}
			else{
				var e:TemplateEvent = new TemplateEvent("openTemplate");
				e.templateId = item.data["id"];
				e.templateName = item.data["name"];
				Context.container.dispatchEvent(e);
			}
			event.preventDefault();
		}
		
//		protected function doubleClickHandler(event:MouseEvent):void
//		{
//			var item:IListItemRenderer = event.target as IListItemRenderer;
//			if(item == null){
//				trace("no item selected");
//			}
//			else{
//
//			}
//			
//		}
	}
}